package com.jetinno.syrup2

import com.jetinno.hex.HexTrans

/**
 * Created by zhangyuncai on 2023/3/15.
 * E30100000900
 */
class SryupQuery {

}

///参照协议
class SyrupQuerySend {
    private val head = 0xE3 //请求头
    private val funcType = 0x01 //查询状态类型
    private val state = 0x00 //查询状态
    private val reserve1 = 0x00 //保留
    private val length = 0x09 //返回数据长度
    private val startAddr = 0x00 //数据起始地址
}

class SyrupQueryReceive constructor(receive: String? = null) {
    var version = 0//版本号
    var systemBusy = false//bit0~bit7  1:忙 0：不忙 (bit0~bit5 代表6个糖浆电机 bit6:代表分杯套模组)
    private var faultType = 0//故障H 故障类型
    private var faultL = 0//故障L 故障对应电机
    var onlineMotorList = arrayListOf<Int>()//电机在线传感器

    var faultCode: String? = null
    var hasCupSleeve = false//是否已有被套

    init {
        if (!receive.isNullOrEmpty()) {
            val ints = HexTrans.hexString2Ints(receive)
            if (ints.size > 5) {
                version = ints[0]
                systemBusy = (ints[1] > 0)
                faultType = ints[2]
                faultL = ints[3]


                when (faultType) {
                    0x00 -> {//电机正常
                        faultCode = null
                    }
                    0x90 -> {//该故障表示电路板过流
                        faultCode = "M9000"
                    }
                    0x91 -> {//1：堵转 0：不堵转；每一个bit代表一个电机
                        faultCode = getFaultCode(faultL, FAULTS_91)
                    }
                    0x92 -> {//1：超时 0：不超时；每一个bit代表一个电机
                        faultCode = getFaultCode(faultL, FAULTS_92)
                    }
                    0x93 -> {//该故障表示缺杯套
                        faultCode = "M9300"
                    }
                    0x94 -> {//该故障表示分杯套失败
                        faultCode = "M9400"
                    }
                    0x95 -> {//该故障表示分被套模组 后 位置检测的传感器有故障
                        faultCode = "M9500"
                    }
                    0x96 -> {//该故障表示分被套模组 前 位置检测的传感器有故障
                        faultCode = "M9600"
                    }
                }

                //得到位数组再倒序，这样角标0表示糖浆电机1
                onlineMotorList.clear()
                val onlineBits = HexTrans.hexStrs2Bits(String.format("%02X", ints[4]))
                onlineBits.reverse()
                var onlineIndex = 0
                for (bit in onlineBits) {
                    if (bit > 0) {
                        onlineMotorList.add(onlineIndex)
                    }
                    onlineIndex++
                }

                val ints_5 = ints[5]
                val ints_5_3 = HexTrans.getChildBit(ints_5, 3)
                hasCupSleeve = ints_5_3 == 1
            }
        }
    }

    override fun toString(): String {
        return "SyrupQueryReceive(version=$version, systemBusy=$systemBusy, onlineMotorList=$onlineMotorList, faultCode=$faultCode)"
    }


}

fun getFaultCode(faultL: Int, faultLValues: Array<String>): String? {
    var faultCode: String? = null
    //得到位数组再倒序，这样角标0表示糖浆电机1
    val bits = HexTrans.hexStrs2Bits(String.format("%02X", faultL))
    bits.reverse()
    var index = 0
    for (bit in bits) {
        if (bit > 0) {
            faultCode = faultLValues[index]
            break
        }
        index++
    }
    return faultCode
}

val FAULTS_91 = arrayOf("M9101", "M9102", "M9104", "M9108", "M9110", "M9120", "M9140", "M9180")
val FAULTS_92 = arrayOf("M9201", "M9202", "M9204", "M9208", "M9210", "M9220", "M9240", "M9280")

/*
M9101	糖浆电机1堵转
M9102	糖浆电机2堵转
M9104	糖浆电机3堵转
M9108	糖浆电机4堵转
M9110	糖浆电机5堵转
M9120	糖浆电机6堵转
M9140	糖浆电机7堵转
M9180	糖浆电机8堵转

M9201	糖浆电机1动作超时
M9202	糖浆电机2动作超时
M9204	糖浆电机3动作超时
M9208	糖浆电机4动作超时
M9210	糖浆电机5动作超时
M9220	糖浆电机6动作超时
M9240	糖浆电机7动作超时
M9280	糖浆电机8动作超时

M9701	糖浆电机1动作短路
M9702	糖浆电机2动作短路
M9704	糖浆电机3动作短路
M9708	糖浆电机4动作短路
M9710	糖浆电机5动作短路
M9720	糖浆电机6动作短路
M9740	糖浆电机7动作短路
M9780	糖浆电机8动作短路

M9801	糖浆电机1动作开路
M9802	糖浆电机2动作开路
M9804	糖浆电机3动作开路
M9808	糖浆电机4动作开路
M9810	糖浆电机5动作开路
M9820	糖浆电机6动作开路
M9840	糖浆电机7动作开路
M9880	糖浆电机8动作开路

M9900	糖浆板未连接
*/